package edu.hawaii.its381b.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.hawaii.its381b.shared.Score;

/**
 * @author David Nickles
 * @date   May 17, 2012
 */
public class Scoreboard extends Composite {

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private static final GreetingServiceAsync scoreboardService = GWT
	.create(GreetingService.class);

	private static final int PANELINSET = 25;
	private static final int FRAMEINSET = 50;

	private VerticalPanel panel;

	/**
	 * 
	 */
	public Scoreboard(Score score) {
		super();
		int windowWidth = Window.getClientWidth();
		int windowHeight = Window.getClientHeight();
		panel = new VerticalPanel();
		panel.setWidth((windowWidth-PANELINSET)+"px");
		panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		panel.setStyleName("mainBackground");

		final HTML html = new HTML();
		html.setHTML("<h2>Loading...</h2>");
		html.setStyleName("gwt-Label-Loading");
		html.setSize((windowWidth-FRAMEINSET)+"px", (windowHeight-FRAMEINSET*2-PANELINSET)+"px");
		panel.add(html);

		final Button closeButton = new Button("Close");
		closeButton.setStyleName("gwt-Button-Actions");
		panel.add(closeButton);

		scoreboardService.putScore(score,
				new AsyncCallback<String>() {

					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						html.setText("Unable to contact the scoreboard web service. " + SERVER_ERROR);
						html.addStyleName("serverResponseLabelError");
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						html.removeStyleName("serverResponseLabelError");
						html.setHTML(result);
						closeButton.setFocus(true);
					}
				});

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				startNewGame(); // switch views
			}
		});
		initWidget(panel);
	}

	public void startNewGame() {
		RootPanel.get().clear();
		RootPanel.get().add(new GameEngine());
	}

	public static void recordScore(Score score) {
		scoreboardService.putScore(score,
				new AsyncCallback<String>() {

			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
			}

			public void onSuccess(String result) {

			}
		});
	}
}
